package com.berchen.utils;

import java.io.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 将文件转换为base64字符串：
 *  文件->byte[]
 *  byte[] -> base64字符串（new BASE64Encoder().encode(byte[])）
 *
 * 将base64字符串转换成文件：
 *  base64-> byte[] (new BASE64Decoder().decodeBuffer(base64Code))
 *  byte[] -> 文件
 */
public class FileBase64ConvertUitl {
    /**
     * 将文件转换成BASE64格式
     * 将文件转成base64 字符串  * @param path文件路径  * @return     * @throws Exception
     * @param path  文件路径
     */
    public static String encodeBase64File(String path) {
        FileInputStream inputFile=null;
        try {
            File file = new File(path);
            inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return new BASE64Encoder().encode(buffer);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            if(inputFile!=null){
                try {
                    inputFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     *
     * @param inputStream
     * @return
     */
    public static String encodeBase64File(InputStream inputStream) {
        byte[] data =null;
        ByteArrayOutputStream swapStream = null;
        try {
            byte[] buff = new byte[1024];
            swapStream = new ByteArrayOutputStream();
            int rc = 0;
            while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
            return new BASE64Encoder().encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(swapStream!=null){
                try {
                    swapStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /**
     * 将base64字符解码保存文件
     * @param base64Code
     * @param targetPath
     */
    public static void decoderBase64File(String base64Code, String targetPath)  {
        FileOutputStream out=null;
        try {
            byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
             out= new FileOutputStream(targetPath);
            out.write(buffer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(out!=null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 将base64字符保存文本文件
     * @param base64Code
     * @param targetPath
     */
    public static void toFile(String base64Code, String targetPath){
        byte[] buffer = base64Code.getBytes();
        FileOutputStream out=null;
        try {
            out = new FileOutputStream(targetPath);
            out.write(buffer);
            out.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            String base64Code = encodeBase64File("E:\\1project\\yfwService\\src\\main\\resources\\pdfTemplate.pdf");
            System.out.println(base64Code);
            decoderBase64File(base64Code, "E:\\testFloder\\pdfTemplate.pdf");
            toFile(base64Code, "E:\\testFloder\\presPdf111.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
